package com.jz.community.basecomm.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.mvp.MVPContract;
import com.jz.community.basecomm.utils.ActivityUtil;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RxTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.basecommunity.R;
import com.roger.gifloadinglibrary.GifLoadingView;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import butterknife.ButterKnife;

/**
 * Created by Tony.Fan on 2018/3/15 13:48
 */
public abstract class BaseMvpActivity<V extends MVPContract.View, P extends BaseMvpPresenter<V>> extends SupportActivity {
    protected Activity mActivity;
    protected P mPresenter;
    private ImmersionBar immersionBar;
    public IWXAPI api;
    protected GifLoadingView gifLoadingView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentView());
        ButterKnife.bind(this);
        mActivity = this;
        ActivityUtil.addActivityToList(this);
        immersionBar = ImmersionBar.with(this);
        initPresenter();
        gifLoadingView = new GifLoadingView();
        gifLoadingView.setBackgroundResource(R.drawable.num19);
        initView();
        addListener();
        initDatas();
    }

    protected void addListener() {

    }

    protected void registerWeChatApp() {
        api = WXAPIFactory.createWXAPI(this, AppConstants.WX_APP_ID, true);
        api.registerApp(AppConstants.WX_APP_ID);
    }

    private void initPresenter() {
        if (Preconditions.isNullOrEmpty(mPresenter)) {
            mPresenter = createPresenter();
            if (!Preconditions.isNullOrEmpty(mPresenter)){
                mPresenter.attachView((V) this);
            }
        }
    }

    /**
     * 状态栏
     *
     * @param orderHomeToolbar
     */
    protected void setImmersionBar(Toolbar orderHomeToolbar) {
        immersionBar
                .titleBar(orderHomeToolbar)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }


    /**
     * 统一标题、返回处理
     *
     * @param titleName
     * @param titleRightName
     */
    public void initTitle(String titleName, String titleRightName) {

        TextView titleTv = findViewById(R.id.title_name);
        ImageView titleBackBtn = findViewById(R.id.title_back_left);
        TextView titleRight = findViewById(R.id.title_right);

        if (!Preconditions.isNullOrEmpty(titleName)) {
            titleTv.setText(titleName);
        }

        if (!Preconditions.isNullOrEmpty(titleRightName)) {
            SHelper.vis(titleRight);
            titleRight.setText(titleRightName);
        } else {
            SHelper.gone(titleRight);
        }
        titleBackBtn.setOnClickListener(v -> finish());
    }

    /**
     * 设置ContentView
     *
     * @return
     */
    protected abstract int getContentView();

    /**
     * 初始化数据
     */
    protected void initDatas() {

    }

    /**
     * 初始化View
     */
    protected void initView() {

    }

    /**
     * 显示Loading对话框
     */
    protected void showLoading() {
        gifLoadingView.show(getFragmentManager(), "");
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                View view = getCurrentFocus();
                RxTool.hideKeyboard(ev, view, mActivity);//调用方法判断是否需要隐藏键盘
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 关闭Loading对话框
     */
    protected void dismissLoading() {
        gifLoadingView.dismiss();
    }


    protected void showNormal(String msg) {
        WpToast.l(mActivity, msg);
    }


    public View noDataView(RecyclerView recyclerView,String msg) {
        View view = getLayoutInflater().inflate(R.layout.view_empty, (ViewGroup) recyclerView.getParent(), false);
        ImageView empty_iv = view.findViewById(R.id.empty_img);
        TextView empty_tv = view.findViewById(R.id.empty_tv);
        empty_iv.setImageResource(R.mipmap.empty_default);
        empty_tv.setText(msg);
        return view;
    }

    protected abstract P createPresenter();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (immersionBar != null) {
            immersionBar.destroy();  //不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
        }
    }
}
